//
//  selectView.m
//  EasyToTesco
//
//  Created by ym on 16/3/28.
//  Copyright © 2016年 Jack. All rights reserved.
//

#import "shopselectView.h"

@interface shopselectView ()
@property (nonatomic, strong)UIView *lineview;
@property (nonatomic, strong)UIColor *unselectedColor;
@property (nonatomic, strong)UIColor *selectedColor;

@property (nonatomic,strong)NSMutableArray *buttonArray;

@end

@implementation shopselectView

- (NSMutableArray *)buttonArray {
    if (!_buttonArray) {
        _buttonArray = [NSMutableArray array];
    }
    return _buttonArray;
}

- (instancetype)initWithFrame:(CGRect)frame {
    self = [super initWithFrame:frame];
    if (self) {
        self.titleArray = [NSMutableArray arrayWithObjects:@"积分权", @"代金券", nil];
        self.selectedColor = DEFAULT_COLOR;
        self.unselectedColor = [UIColor colorWithRed:181/255. green:180/255. blue:180/255. alpha:1];
        self.userInteractionEnabled = YES;
        [self addSubViewUI];
        
        [self addSublineView];
    }
    return self;
}

- (void)addSublineView {
    UIView *centerXView = [[UIView alloc] init];
    centerXView.backgroundColor = self.unselectedColor;
    centerXView.alpha = 0.1;
    [self addSubview:centerXView];
    
    [centerXView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.equalTo(self.mas_centerX);
        make.top.equalTo(self.mas_top).with.offset(3);
        make.width.mas_equalTo(1);
        make.height.mas_equalTo(HEIGHT * 0.07 - 6);
    }];
    
//    self.lineview = centerXView;
    
}

- (void)addSubViewUI {
    
    CGFloat weight = CGRectGetWidth(self.frame)/self.titleArray.count;
    CGFloat height = CGRectGetHeight(self.frame);
    for (NSInteger i = 0; i < self.titleArray.count; i++) {
        UIButton *titleButton = [UIButton buttonWithType:UIButtonTypeCustom];
        titleButton.frame = CGRectMake(weight * i, 0, weight, height);
        titleButton.titleLabel.textAlignment = NSTextAlignmentCenter;
        titleButton.tag = 1000+i;
        titleButton.backgroundColor = [UIColor whiteColor];
        titleButton.titleLabel.font = [UIFont systemFontOfSize:17];
        titleButton.titleLabel.font = [UIFont fontWithName:@ "Arial Rounded MT Bold"  size:(18.0)];
        [titleButton setTitle:self.titleArray[i] forState:UIControlStateNormal];
        [titleButton addTarget:self action:@selector(selectButtonClick:) forControlEvents:UIControlEventTouchUpInside];
        [titleButton setTitleColor:self.unselectedColor forState:UIControlStateNormal];
        if (i==0) {
            [titleButton setTitleColor:self.selectedColor forState:UIControlStateNormal];
        }
        [self.buttonArray addObject:titleButton];
        [self addSubview:titleButton];
    }
    
    UIView *bottomXView = [[UIView alloc] init];
    bottomXView.backgroundColor = DEFAULT_COLOR;
    bottomXView.alpha = 0.5;
    [self addSubview:bottomXView];
    
    [bottomXView setFrame:CGRectMake(25, height - 4, weight - 50, 4)];
    
    self.lineview = bottomXView;
    
}

- (void)selectButtonClick:(UIButton *)button {
    
    [UIView animateWithDuration:0.5 animations:^{
        CGPoint labelC = self.lineview.center;
        labelC = button.center;
        self.lineview.center = labelC;
        
        CGRect labelF = self.lineview.frame;
        labelF.origin.y = self.frame.size.height - labelF.size.height;
        self.lineview.frame = labelF;
        
        //        NSLog(@"%zd",btn.tag);
        
    }];
    
    for (UIButton *tempBtn in self.buttonArray) {
//        if (tempBtn.tag == button.tag) {
//            [tempBtn setTitleColor:self.selectedColor forState:UIControlStateNormal];
////            self.lineview.backgroundColor = [UIColor blackColor];
//        }else{
//            [tempBtn setTitleColor:self.unselectedColor forState:UIControlStateNormal];
//            
////            self.lineview.backgroundColor = [UIColor blackColor];
//        }
        [tempBtn setTitleColor:self.unselectedColor forState:UIControlStateNormal];
        if (tempBtn.tag == 1000) {
            [button setTitleColor:self.selectedColor forState:UIControlStateNormal];
        } else {
            [button setTitleColor:self.selectedColor forState:UIControlStateNormal];
        }
        
    }
//    [button setTitleColor:self.unselectedColor forState:UIControlStateNormal];
//    if (button.tag == 1000) {
//        [button setTitleColor:self.selectedColor forState:UIControlStateNormal];
//    } else {
//        [button setTitleColor:self.selectedColor forState:UIControlStateNormal];
//    }
    
    [self.delegate selectBtnClick:button];
}

/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/

@end
